#include "status_bar.h"

status_bar_t *status_bar_create(lv_obj_t *parent) {
    // 分配内存
    status_bar_t *status_bar = lv_mem_alloc(sizeof(status_bar_t));
    if (status_bar == NULL) {
        return NULL;
    }
    lv_memset(status_bar, 0, sizeof(status_bar_t));
    
    // 创建状态栏对象
    status_bar->obj = lv_obj_create(parent);
    if (status_bar->obj == NULL) {
        lv_mem_free(status_bar);
        return NULL;
    }
    
    // 设置状态栏样式
    lv_obj_set_size(status_bar->obj, LV_PCT(100), 30);
    lv_obj_align(status_bar->obj, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_style_bg_color(status_bar->obj, lv_color_hex(0x000000), 0);
    lv_obj_set_style_bg_opa(status_bar->obj, LV_OPA_40, 0);
    
    // 创建WiFi图标
    status_bar->wifi_icon = lv_label_create(status_bar->obj);
    lv_label_set_text(status_bar->wifi_icon, LV_SYMBOL_WIFI);
    lv_obj_align(status_bar->wifi_icon, LV_ALIGN_LEFT_MID, 10, 0);
    
    // 创建电池图标
    status_bar->battery_icon = lv_label_create(status_bar->obj);
    lv_label_set_text(status_bar->battery_icon, LV_SYMBOL_BATTERY_FULL);
    lv_obj_align(status_bar->battery_icon, LV_ALIGN_RIGHT_MID, -10, 0);
    
    // 创建通知图标
    status_bar->notify_icon = lv_label_create(status_bar->obj);
    lv_label_set_text(status_bar->notify_icon, LV_SYMBOL_BELL);
    lv_obj_align(status_bar->notify_icon, LV_ALIGN_RIGHT_MID, -40, 0);
    
    return status_bar;
}

void status_bar_set_wifi_state(status_bar_t *status_bar, bool connected) {
    if (status_bar == NULL || status_bar->wifi_icon == NULL) {
        return;
    }
    
    lv_label_set_text(status_bar->wifi_icon, 
                      connected ? LV_SYMBOL_WIFI : LV_SYMBOL_CLOSE);
}

void status_bar_set_battery_state(status_bar_t *status_bar, uint8_t level) {
    if (status_bar == NULL || status_bar->battery_icon == NULL) {
        return;
    }
    
    const char *battery_symbol;
    if (level >= 90) {
        battery_symbol = LV_SYMBOL_BATTERY_FULL;
    } else if (level >= 60) {
        battery_symbol = LV_SYMBOL_BATTERY_3;
    } else if (level >= 40) {
        battery_symbol = LV_SYMBOL_BATTERY_2;
    } else if (level >= 20) {
        battery_symbol = LV_SYMBOL_BATTERY_1;
    } else {
        battery_symbol = LV_SYMBOL_BATTERY_EMPTY;
    }
    
    lv_label_set_text(status_bar->battery_icon, battery_symbol);
}

void status_bar_set_notify_state(status_bar_t *status_bar, bool has_notify) {
    if (status_bar == NULL || status_bar->notify_icon == NULL) {
        return;
    }
    
    lv_obj_set_style_text_color(status_bar->notify_icon, 
                               has_notify ? lv_color_hex(0xFF0000) : lv_color_hex(0xFFFFFF),
                               0);
}